package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.ChanPinDaoImpl;
import com.ysd.entity.ChanPin;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminChanPinServlet extends BaseServlet {
	/**
	 * 查询所有产品
	 * localhost:8080/qiuxieshangcheng/adminChanPin?reqName=delChanPin
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	
	public void getChanPin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ChanPinDaoImpl cpDao = new ChanPinDaoImpl();//创建实现类对象
		List<ChanPin>  cpList=  cpDao.getChanPinList1();//查到的所有产品集合
		//发送Ajax请求，服务器基本都会返回json格式字符串
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		String res = gson.toJson(cpList);
		
		out.print(res);
		
		out.flush();
		out.close();
		
	}
	
	
	public void delChanPin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int cpId = Integer.parseInt(request.getParameter("chanpinId"));
		ChanPinDaoImpl cpDao = new ChanPinDaoImpl();
		int cp= cpDao.deleteChanPinId(cpId);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		if (cp>0) {//删除成功
			out.print(Result.toClient(true,"删除成功", ""));//{"success":true,"message":"删除成功"，"remark":""}
		} else {//删除失败
			out.print(Result.toClient(false,"删除失败", ""));//{"success":false,"message":"删除失败"，"remark":""}
		}
		
		out.flush();
		out.close();
		
	}

}
